/**
 * Created by yjp on 2017/10/25.
 */

/**
 * 切换至某个页面。
 * 参数 frm : index.html中的iframe 的 ID。
 * 参数 uri : 相对于index.html的文件路径。
 * 当uri变化时，会先调用 frame.onSrcChanged，然后清除此函数，再跳转UII
 */
function switch_to_frame(frm, uri) {
    var frame = document.getElementById(frm);
    if (frame.src_short != uri){
        if(frame.onSrcChanged){
            try{
                frame.onSrcChanged();
            }
            catch(e){};
            delete frame.onSrcChanged;
        }
        frame.src = uri;
        frame.src_short = uri;
    }
}

/**
 * 分析URL，可以解出类似协议(http)，参数等。必须是标准的URL
 */
function parseURL(url) {
    var a =  document.createElement('a');
    a.href = url;
    return {
        source: url,
        protocol: a.protocol.replace(':',''),
        host: a.hostname,
        port: a.port,
        query: a.search,
        params: (function(){
            var ret = {},
                seg = a.search.replace(/^\?/,'').split('&'),
                len = seg.length, i = 0, s;
            for (;i<len;i++) {
                if (!seg[i]) { continue; }
                s = seg[i].split('=');
                ret[s[0]] = s[1];
            }
            return ret;
        })(),
        file: (a.pathname.match(/\/([^\/?#]+)$/i) || [,''])[1],
        hash: a.hash.replace('#',''),
        path: a.pathname.replace(/^([^\/])/,'/$1'),
        relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [,''])[1],
        segments: a.pathname.replace(/^\//,'').split('/')
    };
}

/**
 * 时间过滤选择
*/
function updateDate(str) {
    str = str.replace(/T/,' ');
    str = str.replace(/.000Z/,'');
    var a1;
    var a2;
    a1 = str.split(' ');
    a2 = a1[1].split(':');
    a2[0] =parseInt(a2[0]) + 8;
    if(a2[0] >= 24) {
        a1[0] = a1[0].split('-');
        a1[0][2] = parseInt(a1[0][2]) + 1;
        a1[0] = a1[0].join('-');
        a2[0] = a2[0]-24;
        if(a2[0] < 10) {
            a2[0] = '0'+a2[0];
        }
    }
    a2 = a2.join(':');
    var update_time = a1[0]+' '+a2;
    return update_time;
}

//时间过滤选择
function simpleDateFormat(time) {
    var d = new Date(time);
    var month = d.getMonth() + 1;
    if (month < 10) {
        month = '0' + month;
    }
    var date = d.getDate();
    if (date < 10) {
        date = '0' + date;
    }
    var hour = d.getHours();
    if (hour < 10) {
        hour = '0' + hour;
    }
    var minute = d.getMinutes();
    if (minute < 10) {
        minute = '0' + minute;
    }
    var second = d.getSeconds();
    if (second < 10) {
        second = '0' + second;
    }
    return d.getFullYear() + '-' + month + '-' + date + ' ' + hour + ':' + minute + ':' + second;
}

/*图片URL替换函数*/
function getObjectURL(file) {
    var url = null ;
    if (window.createObjectURL!=undefined) { // basic
        url = window.createObjectURL(file) ;
    } else if (window.URL!=undefined) { // mozilla(firefox)
        url = window.URL.createObjectURL(file) ;
    } else if (window.webkitURL!=undefined) { // webkit or chrome
        url = window.webkitURL.createObjectURL(file) ;
    }
    return url ;
}

// HTML实体编码
function escapeHtml(string) {
    var entityMap = {
        "&": "&amp;",
        "<": "&lt;",
        ">": "&gt;",
        '"': '&quot;',
        "'": '&#39;',
        "/": '&#x2F;'
    };
    return String(string).replace(/[&<>"'\/]/g, function (s) {
        return entityMap[s];
    });
}

// 图片地址修改
function picUrlTrans(picUrl, url) {
    if(picUrl.indexOf('http') >= 0) 
        return picUrl;
    else 
        return url + picUrl;
}